package boun.swe.bob.entities;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: ememory
 * Date: 27.Ara.2010
 * Time: 15:22:01
 * To change this template use File | Settings | File Templates.
 */
/**
 * This entity corresponds to user_message table in db
 */
@Entity
@Table(name = "events")
public class Bob_Event {

    @Id
    @GenericGenerator(name = "generator", strategy = "increment")
    @GeneratedValue(generator = "generator")
    @Column(name = "event_id")
    private Integer eventId;

    @Column(name = "event_name")
    private String eventName;

    @Column(name = "description")
    private String description;

    @Column(name = "create_date")
    private Timestamp createDate;


    @Column(name = "start_date")
    private Timestamp startDate;


    @Column(name = "end_date")
    private Timestamp endDate;

    @Column(name = "location")
    private String location;

    @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "owner_user_id")
    private User ownerUser;

    @ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "events")
    private Set<User> members;

    @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "event_id")
    @OrderBy("feedId DESC")
    private Set<EventFeed> eventFeeds;

    public Bob_Event()
    {}
    public Bob_Event(Integer eventId, String eventName, String description, Timestamp createDate, Timestamp startDate, Timestamp endDate, String location, User ownerUser, Set<User> members, Set<EventFeed> eventFeeds) {
        this.eventId = eventId;
        this.eventName = eventName;
        this.description = description;
        this.createDate = createDate;
        this.startDate = startDate;
        this.endDate = endDate;
        this.location = location;
        this.ownerUser = ownerUser;
        this.members = members;
        this.eventFeeds = eventFeeds;
    }

    public Integer getEventId() {
        return eventId;
    }

    public void setEventId(Integer eventId) {
        this.eventId = eventId;
    }

    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Timestamp getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

    public Timestamp getStartDate() {
        return startDate;
    }

    public void setStartDate(Timestamp startDate) {
        this.startDate = startDate;
    }

    public Timestamp getEndDate() {
        return endDate;
    }

    public void setEndDate(Timestamp endDate) {
        this.endDate = endDate;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public User getOwnerUser() {
        return ownerUser;
    }

    public void setOwnerUser(User ownerUser) {
        this.ownerUser = ownerUser;
    }

    public Set<User> getMembers() {
        return members;
    }

    public void setMembers(Set<User> members) {
        this.members = members;
    }

    public Set<EventFeed> getEventFeeds() {
        return eventFeeds;
    }

    public void setEventFeeds(Set<EventFeed> eventFeeds) {
        this.eventFeeds = eventFeeds;
    }

      public List<User> getTopMembersList() {
        int limit = Math.min(members.size(), 10);
        List<User> list = new ArrayList<User>(members).subList(0, limit);
        return list;
    }

    public List<EventFeed> getTopFeedsList(){
        int limit= Math.min(eventFeeds.size(),15);
        List<EventFeed> list = new ArrayList<EventFeed>(eventFeeds).subList(0, limit);
        return list;
    }
    public void addMember(User member)
    {
        this.members.add(member);
    }
}